<?php
            
use app\model\Theme; 


/**
 * Author: cbw
 * Desc: 教培
 * Time: 2021/11/15 3:02 下午
 * Email: 17854288794@163.com
 */
class Gift_Page extends PluginWebPage {
    function main()
    {
        global $_G,$_P,$_S,$_C;
        $action = 6;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $type = isset($_P['type'])?$_P['type']:1;
        $type_arr = [
            ['title'=>'礼品','type'=>1,'total'=>$this->getTypeTotal($themeid,1)],
            ['title'=>'赠品','type'=>0,'total'=>$this->getTypeTotal($themeid,0)]
        ];
        $themelist = Theme::getAllTheme();
        $name = isset($_P['name'])?trim($_P['name']):'';
        $page_index = max(1,intval($_P['page']));
        $page_size = 10;
        $page_start = ($page_index-1)*$page_size;
        $shangjiaid = isset($_P['shangjiaid'])?$_P['shangjiaid']:0;
        $shangjia = EducationModel::getShangjiaList();
        array_unshift($shangjia,['id'=>0,'name'=>'全部商家']);
        $page_total = EducationModel::getGiftList($themeid,$shangjiaid,$name,0,0,$type,-1,true);
        $list = EducationModel::getGiftList($themeid,$shangjiaid,$name,$page_start,$page_size,$type);
        $total = $page_total;
        $pager = pagination($page_total,$page_index,$page_size);
        $item = EducationModel::getRewardByWhere(['themeid'=>$themeid]);
        $open_shangjia_rules = $item['open_shangjia_rules'];
        $return_clicent = get_defined_vars();
return $this->view('/education/gift/main', $return_clicent);
    }

    function add()
    {
        return $this->post();
    }
    function edit()
    {
        return $this->post();
    }

    protected function post()
    {
        global $_G, $_P,$_S,$_C;
        $action = 6;
        $system = $_S;
        $config = $_C;
        $id = intval($_P['id']);
        $themeid = intval($_P['themeid']);
        $shangjiaid = intval($_P['shangjiaid']);
        $shangjia = EducationModel::getShangjiaList();
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }

        if ($_G['ispost'] && $themeid ) {
            if (!$shangjiaid){
                show_json(0,'礼品商家不正确');
            }
            $name = trim($_P['name']);
            $top_img = tomedia($_P['top_img']);
            $price = trim($_P['price']);
            $qty = isset($_P['qty'])?$_P['qty']:1000;
            $status = $_P['status'];
            $type = $_P['type'];
            $detail = htmlspecialchars_decode($_P['detail']);
            if (!$name){
                show_json(0,'礼品名称不能为空');
            }
            if (!$top_img){
                show_json(0,'礼品头图未设置');
            }
            if (!$qty){
                show_json(0,'礼品当前库存未设置');
            }
            if (!$price){
                show_json(0,'礼品市场价格未设置');
            }

            $data = [
                'shangjiaid'=>$shangjiaid,
                'name'=>$name,
                'top_img'=>$top_img,
                'qty'=>$qty,
                'price'=>$price,
                'status'=>$status,
                'type'=>$type,
                'themeid'=>$themeid,
                'detail'=>$detail,
                'sort'=>$_P['sort'],
            ];
            if (!$id){

                $res = EducationModel::insertGift($data);
            }else{
                $where = ['id'=>$id];
                $res = EducationModel::editGiftItem($where,$data);
            }
            if (!$res){
                show_json(0,['msg'=>'操作失败']);
            }else{
                show_json(1,['msg'=>'操作成功']);
            }
            die();
        }
        if ($id && $themeid ){
            $item = EducationModel::getGiftItemById($themeid,$id);
        }
        $items = EducationModel::getRewardByWhere(['themeid'=>$themeid]);
        $open_shangjia_rules = $items['open_shangjia_rules'];
        $return_clicent = get_defined_vars();
return $this->view('/education/gift/edit', $return_clicent);
    }

    function editItemQty()
    {
        global $_G, $_P;
        $action = 5;
        $id = intval($_P['id']);
        $themeid = intval($_P['themeid']);
        $op_num = intval($_P['op_num']);
        $action = trim($_P['action']);
        $new_qty = 0;

        $item = EducationModel::getCourseItemById($themeid,$id);
        if ($action == 'add'){
            $new_qty = $item['qty']+$op_num;
        }else{
            $new_qty = max(0,($item['qty']-$op_num));
        }
        $where = ['id'=>$id];
        $data = ['qty'=>$new_qty];
        $res = EducationModel::editCourseItem($where,$data);
        if (!$res){
            show_json(0,['msg'=>'操作失败']);
        }else{
            show_json(1,['msg'=>'操作成功']);
        }
    }

    function editSortItem()
    {
        global $_G,$_S,$_C,$_P;
        $system = $_S;
        $config = $_C;
        $p_id = intval($_P['id']);
        $themeid = $_P['themeid'];
        $action = $_P['action'];
        $sort = intval($_P['sort']);
        $temp_sort = 0;

        if ($action=='down'){
            $where = ['id'=>$p_id,'themeid'=>$themeid];
            $data = ['sort'=>$temp_sort];

            $where1 = ['sort'=>$sort+1,'themeid'=>$themeid];
            $data1 = ['sort'=>$sort];

            $where2 = ['id'=>$p_id,'themeid'=>$themeid];
            $data2 = ['sort'=>$sort+1];
        }
        if ($action=='up'){
            $where = ['id'=>$p_id,'themeid'=>$themeid];
            $data = ['sort'=>$temp_sort];

            $where1 = ['sort'=>$sort-1,'themeid'=>$themeid];
            $data1 = ['sort'=>$sort];

            $where2 = ['id'=>$p_id,'themeid'=>$themeid];
            $data2 = ['sort'=>$sort-1];
        }

        $res = EducationModel::editGiftItem($where,$data);
        $res1 = EducationModel::editGiftItem($where1,$data1);
        $res2 = EducationModel::editGiftItem($where2,$data2);

        if (!$res  || !$res2){
            show_json(0,['msg'=>'操作失败']);
        }else{
            show_json(1,['msg'=>'操作成功']);
        }
    }

    function getTypeTotal($themeid,$status)
    {
        $condition = " and type=$status";
        $sql = " select count(*) as cnt from ".tablename(t_shangjia_gift)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }
}